package com.zgh.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zgh.domain.Group;
import com.zgh.domain.UserMatch;
import com.zgh.mapper.UserMatchMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserMatchService {
    @Autowired
    private UserMatchMapper userMatchMapper;

    /**
     * 查询全部用户匹配
     * @return 集合
     */
    public List<UserMatch> findAll() {
        return userMatchMapper.selectList(null);
    }

    /**
     * 根据所在地和籍贯查询用户匹配
     * @param position 所在地
     * @param nativePlace 籍贯
     * @return 集合
     */
    public List<UserMatch> findUserByNativeAndRegion(String position, String nativePlace) {
        QueryWrapper<UserMatch> userMatchQueryWrapper = new QueryWrapper<>();
        List<UserMatch> userMatchList = null;
        List<String> positionList = JSON.parseArray(position, String.class);
        String province1 = positionList.get(0);
        String city1 = positionList.get(1);
        String area1 = positionList.get(2);
        List<String> nativePlaceList = JSON.parseArray(nativePlace, String.class);
        String province2 = nativePlaceList.get(0);
        String city2 = nativePlaceList.get(1);
        String area2 = nativePlaceList.get(2);
        if ("全部".equals(province1)){
            //所在地的省为空 即查询全部

        }else if("全部".equals(city1)){
            //所在地的省不为空 市为空 即查询全部省
            userMatchQueryWrapper.like("position",province1);
        }else if("全部".equals(area1)){
            //所在地的区为空 即查询全部市
            userMatchQueryWrapper.like("position",province1+"/"+city1);
        }else{
            //都不为空
            userMatchQueryWrapper.eq("position",province1+"/"+city1+"/"+area1);
        }
        if ("全部".equals(province2)){
            //籍贯的省为空 即查询全部

        }else if("全部".equals(city2)){
            //籍贯的省不为空 市为空 即查询全部省
            userMatchQueryWrapper.like("nativePlace",province2);
        }else if("全部".equals(area2)){
            //籍贯的区为空 即查询全部市
            userMatchQueryWrapper.like("nativePlace",province2+"/"+city2);
        }else{
            //都不为空
            userMatchQueryWrapper.eq("nativePlace",province2+"/"+city2+"/"+area2);
        }
        userMatchList = userMatchMapper.selectList(userMatchQueryWrapper);
        return userMatchList;
    }

    /**
     * 按照条件查询用户匹配
     * @param condition 条件
     * @return 集合
     */
    public List<UserMatch> searchGroupByCondition(String condition) {
        QueryWrapper<UserMatch> userMatchQueryWrapper = new QueryWrapper<>();
        for (int i = 0; i < condition.length(); i++) {
            userMatchQueryWrapper.like("nickname",condition.substring(i)).or()
                    .like("nativePlace",condition.substring(i)).or()
                    .like("position",condition.substring(i)).or();
        }
        return userMatchMapper.selectList(userMatchQueryWrapper);
    }

    /**
     * 根据id查询用户匹配
     * @param id id
     * @return 用户
     */
    public UserMatch searchUserMatchById(String id) {
        return userMatchMapper.selectById(id);
    }

    /**
     * 发布用户匹配
     * @param userMatch 用户
     * @return i
     */
    public int postUserMatch(UserMatch userMatch) {
        return userMatchMapper.insert(userMatch);
    }
}
